
$(document).on('click', ".ub-more-img", function() {
    clearInterval(spotTime);
    if (videListIdArr.length != 0) {
        for (var i=0; i < videListIdArr.length; i++) {
            var player = videListIdArr[i];
            player.pause();
            player.unload();
            player.detachMediaElement();
            player.destroy();
            player = null;
        }
    }
    videListIdArr = [];
    $('#custom-overlay').html('');

    var spotId = $(this).data('id');
    $(".ub-madel-scenic").show();
    //景区信息
    spotPop(spotId);
    //人数变化echarts
    peopleTwo(spotId);
    //热力图
    heatMap(spotId);
    //景区监控
    spotMonitor(spotId);
    popTime = setInterval(function () {
        var refreshMin = new Date().getMinutes();
        if (parseInt(refreshMin)%5 == 0) {
            //景区信息
            spotPop(spotId);
            //人数变化echarts
            peopleTwo(spotId);
            //热力图
            heatMap(spotId);
            //景区监控
            spotMonitor(spotId);
        }
    }, 1000*60)
})
// 监控视频
var pointId;
var flvPlayer;
$(document).on('click', ".point-video", function() {
    videListIdArr = [];
    pointId = $(this).data('id');
    $('#point-video-overlay').loading({
        message:"加载中...",
        theme: 'dark'
    });
    $.ajax({
        url: monitorUrl,
        type: 'get',
        data: {point_id: pointId},
        success: function (json) {
            if (json.type == 'success') {
                var data = json.data;
                $('.spot-point-name').text(data.name);
                $(".ub-madel-video").show();
                if (data.video) {
                    $("#my-player").show();
                    $('.no-video').hide();
                    // 监控播放
                    videoElement = document.getElementById('my-player');
                    if (flvjs.isSupported()) {
                        flvPlayer = flvjs.createPlayer({
                            type: 'flv',
                            isLive: true,
                            url: data.video, //<==自行修改
                        },{
                            enableWorker: false, //不启用分离线程
                            enableStashBuffer: false, //关闭IO隐藏缓冲区
                            reuseRedirectedURL: true, //重用301/302重定向url，用于随后的请求，如查找、重新连接等。
                            autoCleanupSourceBuffer: true //自动清除缓存
                        });
                        flvPlayer.attachMediaElement(videoElement);
                        flvPlayer.load(); //加载
                    }
                    $('.ub-handle').show();

                    document.getElementById('my-player').onplay = function() {
                        $('#point-video-overlay').loading('destroy');
                    };

                } else {
                    $('.no-video').show();
                    $("#my-player").hide();
                    $('#point-video-overlay').loading('destroy');
                }
            }
        }
    });
});

$('.ub-handle_btn').click(function (){
    var control = $(this).data('control');
    $.get(controlUrl, {control: control, point_id: pointId}, function(json) {

    });
});

$('.ub-close-scenic').click(function() {
    videoList(currentPage);
    spotTime = setInterval(function () {
        videoList(currentPage)
    }, 1000 * 60)
    clearInterval(popTime);
    $(".ub-madel-scenic").hide();
    $("#container").removeAttr("style"); //ie,ff均支持
    $('#container').empty();

    for (var i=0; i < flvPlayerArr.length; i++) {
        var player = flvPlayerArr[i];
        player.pause();
        player.unload();
        player.detachMediaElement();
        player.destroy();
        player = null;
    }
});

$(".ub-close-video").click(function(){
    $(".ub-madel-video").hide();
    flvPlayer.pause();
    flvPlayer.unload();
    flvPlayer.detachMediaElement();
    flvPlayer.destroy();
    flvPlayer = null;
});

// 人数变化
peopleOne();
setInterval(function () {
    var refreshMin = new Date().getMinutes();
    console.log(parseInt(refreshMin)%15 == 0)
    if (parseInt(refreshMin)%15 == 0) {
        peopleOne();
        $.get(weatherUrl, '', function(json) {
            if (json.type == 'success') {
                var data = json.data;
                $('.weather-img').attr('src', '/weather/'+ data.img +'.png');
                $('.weather-temp').text(data.temp);
                $('.weather-weather').text(data.weather);
                $('.weather-wS').text(data.wS);
                $('.weather-wD').text(data.wD);
                $('.weather-precipitation').text(data.precipitation);
                $('.weather-quality').text(data.quality);
            }
        });
    }

}, 1000 * 60);
function peopleOne() {
    var peopleOne = echarts.init(document.getElementById('ub-people-one'));
    $.get(peopleChangeUrl, '', function(json) {
        if (json.type == 'success') {
            var data = json.data;
            $('.all_today_real_flow').text(data.todayScenicDayFlow.real_flow == undefined ? 0 : data.todayScenicDayFlow.real_flow);
            $('.all_today_total_flow').text(data.todayScenicDayFlow.total_flow == undefined ? 0 : data.todayScenicDayFlow.total_flow);
            $('.all_yesterday_total_flow').text(data.yesterdayScenicDayFlow.total_flow == undefined ? 0 : data.yesterdayScenicDayFlow.total_flow);
            $('.all_week_real_flow').text(data.weekScenicDayFlow.total_flow == undefined ? 0 : data.weekScenicDayFlow.total_flow);

            $('.ub-update_time').text(data.updateTime);

            optionPeopleOne = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['本周人数', '上周人数'],
                    textStyle:{
                        color:"#fff"
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },

                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.week,
                    axisLine: {
                        lineStyle: {
                            color: '#fff'
                        },
                    }
                },
                yAxis: {
                    type: 'value',
                    axisLine: {
                        lineStyle: {
                            color: '#fff'
                        },
                    },
                    splitLine: {
                        show: false
                    }
                },
                series: [{
                    name: '本周人数',
                    type: 'line',
                    smooth: true,
                    data: data.thisWeekData,
                    itemStyle: {
                        normal: {
                            lineStyle: {
                                color: "#FFEA00"
                            }
                        }
                    },
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#7E5B2C'
                        }, {
                            offset: 1,
                            color: '#524231'
                        }])
                    },
                },
                    {
                        name: '上周人数',
                        type: 'line',
                        smooth: true,
                        data: data.lastWeekData,
                        itemStyle: {
                            normal: {
                                lineStyle: {
                                    color: "#3FFFEA"
                                }
                            }
                        },
                        areaStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: '#05609A'
                            }, {
                                offset: 1,
                                color: '#1E3546'
                            }])
                        },
                    }
                ]
            }
            peopleOne.setOption(optionPeopleOne);
            window.addEventListener("resize", function() {
                peopleOne.resize();
            });
        }
    });
}

// 弹窗人数变化
function peopleTwo(spotId) {
    var peopleTwoEcharts = echarts.init(document.getElementById('ub-people-two'));

    $.get(peopleChangeUrl, {spot_id: spotId}, function(json) {
        if (json.type == 'success') {
            var data = json.data;
            optionPeopleTwo = {
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['本周人数', '上周人数'],
                    textStyle:{
                        color:"#fff"
                    }
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },

                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: data.week,
                    axisLine: {
                        lineStyle: {
                            color: '#fff'
                        },
                    }
                },
                yAxis: {
                    type: 'value',
                    axisLine: {
                        lineStyle: {
                            color: '#fff'
                        },
                    },
                    splitLine: {
                        show: false
                    }

                },
                series: [{
                    name: '本周人数',
                    type: 'line',
                    smooth: true,
                    data: data.thisWeekData,
                    itemStyle: {
                        normal: {
                            lineStyle: {
                                color: "#FFEA00"
                            }
                        }
                    },
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 0,
                            color: '#7E5B2C'
                        }, {
                            offset: 1,
                            color: '#524231'
                        }])
                    },
                },
                    {
                        name: '上周人数',
                        type: 'line',
                        smooth: true,
                        data: data.lastWeekData,
                        itemStyle: {
                            normal: {
                                lineStyle: {
                                    color: "#3FFFEA"
                                }
                            }
                        },
                        areaStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                                offset: 0,
                                color: '#05609A'
                            }, {
                                offset: 1,
                                color: '#1E3546'
                            }])
                        },
                    }
                ]
            }
            console.log(optionPeopleTwo)
            peopleTwoEcharts.setOption(optionPeopleTwo);
            window.addEventListener("resize", function() {
                peopleTwoEcharts.resize();
            });
        }
    });
}


